# Code for Table G1: Descriptive Statistics

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
#  Construct additional variables for table
## ---------------------------------------
# more than 1 year detained
data0812$daysjail1yr <- ifelse(data0812$days_jail > 365, 1, 0)

# create indicators
data0812$totOGS2_1 <- ifelse(data0812$totOGS2 == 1, 1, 0)
data0812$totOGS2_2 <- ifelse(data0812$totOGS2 == 2, 1, 0)
data0812$totOGS2_3 <- ifelse(data0812$totOGS2 == 3, 1, 0)

data0812$Black <- ifelse(data0812$race == "Black", 1, 0)
data0812$White <- ifelse(data0812$race == "White", 1, 0)
data0812$Race_Other <- ifelse(data0812$race == "Other", 1, 0)
data0812$Race_Unk <- ifelse(data0812$race == "Unknown", 1, 0)
data0812$white_hispanic <- ifelse(data0812$race2 == "WhiteH", 1, 0)
data0812$white_nonhispanic <- ifelse(data0812$race2 == "WhiteA", 1, 0)

data0812$quants_inc[is.na(data0812$quants_inc)] <- 0
data0812$botQ <- ifelse(data0812$quants_inc == 1, 1,0)
data0812$midQ <- ifelse(data0812$quants_inc == 2, 1,0)
data0812$topQ <- ifelse(data0812$quants_inc == 3, 1,0)

##--------------------------------------
## create split
detained <- subset(data0812, pti == 1)
released <- subset(data0812, pti == 0)

## specify variables for table
vars <- c( 'pti', 'daysjail1yr', 'days_jail',
          'age_2012', 
          'Female', 
          'Black',
          'White',
          'white_nonhispanic',
          'white_hispanic',
          'Race_Other',
          'Race_Unk',
          'botQ',
          'midQ',
          'topQ',
          'any_drug_2', 
          'any_violent_2',
          'any_violent_3',
          'fire_arms_2',
          'any_rob_2', 
          'any_rob_3',
          'any_dui_2', 
          'totOGS2',
          'totOGS2_1',
          'totOGS2_2',
          'totOGS2_3',
          'mis_only',
          'prior_offender_2', 
          'prior_felChar_doc', 
          'prior_violCrime_doc',
          'prior_guilt_doc',
          'firstBailAmt',
          'vote2008',
          'vote2012',
          'noteli08',
          'regis_before' 
)

labels <- c(
  'Detained more than 3 days',
  'Detained more than 1 year',
  'Days Detained',
  'Age',
  "Female", 
  'Black', 
  'White', 
  "Anglo",
  'Hispanic',
  'Other', 
  'Unknown',
  'Bottom Income',
  'Mid Income',
  'Top Income',
  'Drug',
  'Violent Offense',
  'Violent (expanded)',
  'Firearm Charge',
  "Property",
  'Property (expanded)',
  'DUI', 
  'total OGS',
  'Offense Severity 1',
  'Offense Severity 2',
  'Offense Severity 3',
  'Misdemeanor Charge Only',
  'Prior Case',
  'Prior Cases with Felony Charges',
  'Prior Cases with Violent Charges',
  'Prior Convictions',
  'Bail Amount',
  "Turnout in 2008",
  "Turnout in 2012",
  "Not Elegible in 2008",
  'Registered Before Case' 
)
meanDetained <- sdDetained <- meanReleased  <- sdReleased <- meanAll <- sdAll <- list()

for(n in 1:length(vars)[1]){
  i = vars[n]
  if(i != "firstBailAmt"){
    
    meanDetained[[n]] <- round(mean(detained[[i]], na.rm=T), 2)
    meanReleased[[n]] <- round(mean(released[[i]], na.rm=T), 2)
    meanAll[[n]] <- round(mean(data0812[[i]], na.rm=T), 2)
    
    sdDetained[[n]] <- round(sd(detained[[i]], na.rm=T), 2)
    sdReleased[[n]] <- round(sd(released[[i]], na.rm=T), 2)
    sdAll[[n]] <- round(sd(data0812[[i]], na.rm=T), 2)
  }else{
    meanDetained[[n]] <- round(mean(detained[[i]], na.rm=T))
    meanReleased[[n]] <- round(mean(released[[i]], na.rm=T))
    meanAll[[n]] <- round(mean(data0812[[i]], na.rm=T))
    
    sdDetained[[n]] <- round(sd(detained[[i]], na.rm=T))
    sdReleased[[n]] <- round(sd(released[[i]], na.rm=T))
    sdAll[[n]] <- round(sd(data0812[[i]], na.rm=T))
  }
}
df <-  as.data.frame(cbind(vars, meanAll, meanDetained, meanReleased))
colnames(df) <- c("Variable", "All", "Detained",'Released')


# print table
cf <- as.data.frame(cbind(vars, labels, meanAll, sdAll, meanDetained, sdDetained, meanReleased, sdReleased))
cf[nrow(cf) + 1,] = c("", "Observations",   dim(data0812)[1], "",  dim(detained)[1],"",   dim(released)[1], "")
cat("\nPrinting Table G1: Descriptive Statistics\n")
print(cf)
